//data to displays as a match day

let teams = [{
    logo: "images/17.png",
    teamName: "21软开1",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 0,
},
{
    logo: "images/18.png",
    teamName: "23软开2",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 1,
}, 
{
    logo: "images/11.png",
    teamName: "23区块链",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 2,
}, 
{
    logo: "images/13.png",
    teamName: "23大数据1",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 3,
}, 
{
    logo: "images/12.png",
    teamName: "23大数据2",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 4,
}, 
{
    logo: "images/United.png",
    teamName: "22软开1",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 5,
}, 
{
    logo: "images/10.png",
    teamName: "23软开1",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 6,
}, 
{
    logo: "images/niuyue.png",
    teamName: "22大数据",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 7,
}, 
{
    logo: "images/Chelsea.png",
    teamName: "22软开2",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 8,
}, 
{
    logo: "images/liverpool.png",
    teamName: "23大数据管理",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 9,
}, 
{
    logo: "images/5.png",
    teamName: "21大数据1",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 10,
},
{
    logo: "images/7.png",
    teamName: "21软开3",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 11,
},
{
    logo: "images/15.png",
    teamName: "22区块链",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 12,
},
{
    logo: "images/Arsenal.png",
    teamName: "23软开2&3",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 13,
},
{
    logo: "images/city.png",
    teamName: "21区块链",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 14,
},
{
    logo: "images/HM.png",
    teamName: "21软开4",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 15,
},
{
    logo: "images/FCB.jpg",
    teamName: "21软开2",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 16,
},
{
    logo: "images/9.png",
    teamName: "23大数据2",
    win: 0,
    lose: 0,
    draw: 0,
    goal: 0,
    lost: 0,
    grade: 0,
},

{
    logo: "images/20.png",
    teamName: "学生队"
},

{
    logo: "images/201.png",
    teamName: "老师队"
}
]

//array of weekdays an  months of the actual year
let months = ["一月", "二月", "三月", "四月", "五月", "六月",
    "七月", "八月", "九月", "十月", "十一月", "十二月"
],
    days = ["日", "一", "二", "三", "四", "五", "六"],

    weekDays = document.getElementsByClassName("week-day");

let matchday = document.getElementsByClassName("match");
let teamscore = document.getElementsByClassName("team-score");

const WEEK_CONTAINER = document.getElementById("week-container"),
    WEEK_RANGE = document.getElementById("week-range"),
    WEEK_MATCHES = document.getElementById("week-matches")
var curDate = new Date(2023,9,prompt("你所查询的是哪天的赛程？(若查询失误，请刷新再填)","28"));// 千万注意prompt("你所查询的是哪天的赛程？(若查询失误，请刷新再填)")
    
let baseDay;

let calculateWeekDays = (function () {

    //calculate actual wekk and its week days
    let baseDate = new Date(curDate.getFullYear(), 0, 1),
        dateDiff = Math.floor((curDate - baseDate) / 86400000),
        week = Math.ceil(((dateDiff + curDate.getDay() + 2) / 7));

    if (curDate.getDay() >= 0 && curDate.getDay() <= 2) week += 1

    baseDay = new Date(baseDate.getFullYear(), baseDate.getMonth(), (((week - 1) * 7) - 6));
    let addDays = 6
    let i = 0;
    for (i; i < 7; i++) {
        let dayNumber = baseDay.getDate() + i,
            dayName = days[baseDay.getDay() + i],
            secondPartMonth = baseDay.getMonth(),
            isLastDay = false

        //Validations for the last day of a month based on odds  and pairs index of the months
        function checker() {
            if (baseDay.getDate() + i >= 29 && baseDay.getMonth() % 2 != 0 && baseDay.getMonth() === 1) dayNumber = baseDay.getDate() + i - 28, isLastDay = true

            if (baseDay.getDate() + i >= 31 && baseDay.getMonth() % 2 == 0 && baseDay.getMonth() > 7) dayNumber = baseDay.getDate() + i - 30, isLastDay = true

            if (baseDay.getDate() + i >= 31 && baseDay.getMonth() % 2 != 0 && baseDay.getMonth() < 7) dayNumber = baseDay.getDate() + i - 30, isLastDay = true

            if (baseDay.getDate() + i >= 32 && baseDay.getMonth() % 2 == 0 && baseDay.getMonth() < 7) dayNumber = baseDay.getDate() + i - 31, isLastDay = true

            if (baseDay.getDate() + i >= 32 && baseDay.getMonth() % 2 != 0 && baseDay.getMonth() > 7) dayNumber = baseDay.getDate() + i - 31, isLastDay = true

            if (baseDay.getDate() + i >= 32 && baseDay.getMonth() === 7) dayNumber = baseDay.getDate() + i - 31, isLastDay = true

            return isLastDay
        }

        checker()

        if (checker()) secondPartMonth += 1
        //printing actual weekdays range
        WEEK_RANGE.innerHTML =
            ` ${'2023 年 10月15日 至 11月19日'}`
        //printing  weekdays
        WEEK_CONTAINER.insertAdjacentHTML(
            "beforeend",
            `<div  onclick="selectWeekDay(this)" class="week-day unselected ${days[curDate.getDay()] == dayNumber ? "selected" : ''}">
            <div class="day-number">
            ${dayNumber}
            </div>
            <div class="day-name">
            ${dayName}
        </div>
        `

        )

    }
    WEEK_CONTAINER.insertAdjacentHTML("beforebegin", `<div class="rade">
    
</div>`)
})()

let matches = [{
    teamOne: teams[16],
    teamTwo: teams[13],
    goalone: 2,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 9, 21, 10, 30),
    Isend: true
},
    
{
    teamOne: teams[11],
    teamTwo: teams[14],
    goalone: 0,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 9, 21, 14, 0),
    Isend: true
},

{
    teamOne: teams[2],
    teamTwo: teams[15],
    goalone: 3,
    goaltwo: 7,
    date: new Date(baseDay.getFullYear(), 9, 21, 15, 30),
    Isend: true
},

{
    teamOne: teams[5],
    teamTwo: teams[3],
    goalone: 6,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 9, 22, 9, 0),
    Isend: true
},

{
    teamOne: teams[8],
    teamTwo: teams[6],
    goalone: 0,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 9, 22, 10, 30),
    Isend: true
},
    
{
    teamOne: teams[9],
    teamTwo: teams[0],
    goalone: 6,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 9, 22, 14, 0),
    Isend: true
},

{
    teamOne: teams[12],
    teamTwo: teams[4],
    goalone: 2,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 9, 22, 15, 30),
    Isend: true
},

{
    teamOne: teams[5],
    teamTwo: teams[10],
    goalone: 3,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 10, 3, 14, 0),
    Isend: false
},

{
    teamOne: teams[14],
    teamTwo: teams[0],
    goalone: 8,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 9, 28, 9, 0),
    Isend: true
},

{
    teamOne: teams[10],
    teamTwo: teams[7],
    goalone: 3,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 10, 2, 15, 30),
    Isend: false
},

{
    teamOne: teams[8],
    teamTwo: teams[13],
    goalone: 3,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 10, 5, 10, 30),
    Isend: false
},

{
    teamOne: teams[3],
    teamTwo: teams[10],
    goalone: 3,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 9, 31, 14, 0),
    Isend: false
},

{
    teamOne: teams[12],
    teamTwo: teams[2],
    goalone: 3,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 9, 31, 15, 30),
    Isend: false
},


// 记得安排赛程
// {
//     teamOne: teams[2],
//     teamTwo: teams[3],
//     goalone: 3,
//     goaltwo: 1,
//     date: new Date(baseDay.getFullYear(), 10, 5, 9, 00),
//     Isend: false
// },

{
    teamOne: teams[3],
    teamTwo: teams[7],
    goalone: 0,
    goaltwo: 6,
    date: new Date(baseDay.getFullYear(), 10, 5, 14, 0),
    Isend: false
},

{
    teamOne: teams[16],
    teamTwo: teams[6],
    goalone: 6,
    goaltwo: 9,
    date: new Date(baseDay.getFullYear(), 10, 5, 9, 0),
    Isend: false
},

{
    teamOne: teams[14],
    teamTwo: teams[9],
    goalone: 3,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 9, 29, 9, 0),
    Isend: true
},

{
    teamOne: teams[4],
    teamTwo: teams[15],
    goalone: 2,
    goaltwo: 6,
    date: new Date(baseDay.getFullYear(), 9, 29, 10, 30),
    Isend: true
},

{
    teamOne: teams[5],
    teamTwo: teams[7],
    goalone: 0,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 9, 29, 14, 0),
    Isend: true
},

{
    teamOne: teams[8],
    teamTwo: teams[16],
    goalone: 2,
    goaltwo: 3,
    date: new Date(baseDay.getFullYear(), 9, 29, 15, 30),
    Isend: true
},

{
    teamOne: teams[0],
    teamTwo: teams[11],
    goalone: 1,
    goaltwo: 1,
    date: new Date(baseDay.getFullYear(), 10, 4, 9, 0),
    Isend: false
},

{
    teamOne: teams[12],
    teamTwo: teams[15],
    goalone: 0,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 9, 28, 10, 30),
    Isend: true
},



// {
//     teamOne: teams[4],
//     teamTwo: teams[3],
//     goalone: 3,
//     goaltwo: 2,
//     date: new Date(baseDay.getFullYear(), 10, 12, 10, 30),
//     Isend: false
// },
//大数据实训
// {
//     teamOne: teams[3],
//     teamTwo: teams[10],
//     goalone: 3,
//     goaltwo: 0,
//     date: new Date(baseDay.getFullYear(), 10, 4, 9, 0),
//     Isend: false
// },

{
    teamOne: teams[6],
    teamTwo: teams[13],
    goalone: 1,
    goaltwo: 3,
    date: new Date(baseDay.getFullYear(), 10, 4, 10, 30),
    Isend: false
},

{
    teamOne: teams[9],
    teamTwo: teams[11],
    goalone: 0,
    goaltwo: 3,
    date: new Date(baseDay.getFullYear(), 10, 4, 14, 0),
    Isend: false
},

{
    teamOne: teams[4],
    teamTwo: teams[2],
    goalone: 4,
    goaltwo: 2,
    date: new Date(baseDay.getFullYear(), 10, 4, 15, 30),
    Isend: false
},

{
    teamOne: teams[7],
    teamTwo: teams[9],
    goalone: 3,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 10, 19, 14, 00),
    Isend: false
},

// {
//     teamOne: teams[1],
//     teamTwo: teams[3],
//     goalone: 3,
//     goaltwo: 2,
//     date: new Date(baseDay.getFullYear(), 10, 13, 14, 00),
//     Isend: false
// },

{
    teamOne: teams[2],
    teamTwo: teams[4],
    goalone: 3,
    goaltwo: 0,
    date: new Date(baseDay.getFullYear(), 10, 13, 14, 00),
    Isend: false
},



{
    teamOne: teams[15],
    teamTwo: teams[17],
    goalone: 2,
    goaltwo: 3,
    date: new Date(baseDay.getFullYear(), 10, 12, 16, 00),
    Isend: false
}


]

//showing every match day
for (const key in matches) {
    const element = matches[key];
    WEEK_MATCHES.insertAdjacentHTML("beforeend", `<div data-day="${element.date.getDate()}" data-dayname="${days[element.date.getDay()]}" class="match">
    <div class="match-header">
        开场时间： ${element.date.getHours()}:${element.date.getMinutes() == 0 ? "00" : element.date.getMinutes()}
    </div>
    <div class="match-body">
        <div class="team first-team">
            <div class="team-logo" ><img class="team-logo" src="${element.teamOne.logo }" alt="😥" class="logo" width="90%"></div>
            <div class="team-name">${element.teamOne.teamName}</div>
        </div>

        <div class="versus"><span>VS</span></div>

        <div class="team second-team">
            <div  class="team-logo" ><img src="${element.teamTwo.logo}" alt="😥" class="logo" width="90%"></div>
            <div class="team-name">${element.teamTwo.teamName}</div>
        </div>
    </div>
    <div class="${element.Isend == true ? "team-score" : "unend"} " >
        ${element.goalone}-${element.goaltwo}<br>
        <p align="center">结束</p>
        </div>

    `)
    // if(element.Isend){
    //     WEEK_MATCHES.insertAdjacentHTML("beforeend",`<div class="team-score">
    //     ${element.teamOne.goal}-${element.teamTwo.goal}<br>
    //     <p>已结束</p>
    //     </div>`)
    // }
}

//hiding every match day
function hideMatches(val) {
    for (let index = 0; index < matchday.length; index++) {
        const element = matchday[index];
        element.style.display = val;
    }
}

//selecting matchday based om its day and weekday
function selectWeekDay(node) {
    for (let index = 0; index < weekDays.length; index++) {
        const element = weekDays[index];
        element.classList.add("unselected");
        element.classList.remove("selected");
    }

    node.classList.remove("unselected")
    node.classList.add("selected")
    // Hora
    hideMatches("none");

    for (let index = 0; index < matchday.length; index++) {
        const element = matchday[index];

        if (
            node.children[0].textContent.trim() == element.dataset.day.toString() &&
            node.children[1].textContent.trim() == element.dataset.dayname.toString()
        ) element.style.display = "block"
    }
}



selectWeekDay(weekDays[curDate.getDay()])



function openDialog() {
    document.getElementById('light').style.display = 'block';
}
function closeDialog() {
    document.getElementById('light').style.display = 'none';
}

